<?php
/*
ImgCaptcha - powerful CAPTCHA class written in PHP.
Copyright (C) 2010  Adrian Michalski aka MicMic

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

class imgCaptcha{

	// USER CONFIG
	private $wordlist = 'wordlist/wordlist_en.php';
	// END OF USER CONFIG
	
	private $words;
	private $images = Array();
	private $code;
	private $enteredCode = '';
	private $isCodeCorrect = false;	
	
	// ?
	function __construct(){
		require($this->wordlist);
		$this->words = $words;
		if(session_id() == ''){
			session_start();
		}
	}
	
	// gets random word from array in wordlist
	public function getRandomWord(){
		$random = $this->words;
		$random = $random[array_rand($random)];
		$this->code = $random;
		$this->saveCode();		
	}
	
	// saves code to session
	private function saveCode(){
		$_SESSION['imgcaptcha_code'] = strtolower($this->code);
	}
	
	// returns true if code is valid
	private function validateCode(){
		if(isset($_SESSION['imgcaptcha_code']) && !empty($_SESSION['imgcaptcha_code'])){
			if($_SESSION['imgcaptcha_code'] == strtolower(trim($this->enteredCode))){
				$this->isCodeCorrect = true;
				$_SESSION['imgcaptcha_code'] = '';
			} else {
				$this->isCodeCorrect = false;
			}
		}else{
		  $this->isCodeCorrect = false;
		}
	}
	
	// yeah check this out :>
	public function checkCode($code){
		$this->enteredCode = $code;
		$this->validateCode();
		return $this->isCodeCorrect;
	}
	
	// function that gets images from Bing
	//TODO: add support for more search engines
	private function getImages($word){
		$html = new DomDocument();
		@$html->loadHTMLfile('http://www.bing.com/images/search?q='.$word);
		$params = $html->getElementsByTagName('img');
		$i=0;
		foreach ($params as $param) {
			$images[$i] = $param -> getAttribute('src');
			$i++;
		}
		return $images;
	}

	// returns a random image URL
	public function randomImage(){
		$random = $this->getImages($this->code);
		$random = $random[array_rand($random)];
		return $random;
	}

	// shows image
	public function show(){
		$this->getRandomWord();
		header('Content-type: image/jpeg');
		$im = @imagecreatefromjpeg($this->randomImage());
		if(!$im){
			$im  = imagecreatetruecolor(150, 150);
			$bgc = imagecolorallocate($im, 255, 255, 255);
			$tc  = imagecolorallocate($im, 0, 0, 0);
			imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
			imagestring($im, 1, 5, 5, 'Error while loading image', $tc);
		}
		imagejpeg($im);
		imagedestroy($im);
	}
}
?>
